

package com.googlecode.flac4j.format.frame.sub;


import com.googlecode.jinahya.io.BitInput;
import com.googlecode.jinahya.io.BitOutput;

import java.io.IOException;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;


/**
 *
 * @author Jin Kwon <jinahya at gmail.com>
 */
@XmlType(propOrder = {"value"})
public class CONSTANT extends AbstractSubData {


    /**
     * Creates a new instance.
     */
    public CONSTANT() {
        super(0x00, 0);
    }


    @Override
    public void read(final BitInput input) throws IOException {

        final int bitsPerSample = getFrameHeader().getSampleSize();
        value = input.readUnsignedInt(bitsPerSample);
    }


    @Override
    public void write(final BitOutput output) throws IOException {

        final int bitsPerSample = getFrameHeader().getSampleSize();
        output.writeUnsignedInt(bitsPerSample, value);
    }


    // ------------------------------------------------------------------- value
    /**
     * Returns constant value.
     *
     * @return value
     */
    public int getValue() {

        return value;
    }


    /**
     * Sets constant value.
     *
     * @param value value
     */
    public void setValue(final int value) {

        final int bitsPerSample = getFrameHeader().getSampleSize();
        if (value >> bitsPerSample != 0) {
            throw new IllegalArgumentException(
                "value(" + value + ") >> frame.bitsPerSample(" + bitsPerSample
                + ") != 0");
        }

        this.value = value;
    }


    @XmlElement(required = true)
    private int value;


}

